using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.WebControls;

namespace CommonTagTime
{
    public class clsData
    {
        public bool CheckExist(string FieldName, string TableName, string Condition)
        {
            bool flag = false;
            clsSQL clsSQL = new clsSQL();
            DataTable dt = new DataTable();
            SqlParameter[] para = new SqlParameter[3];
            para[0] = new SqlParameter("@FieldName", SqlDbType.NVarChar);
            para[0].Value = FieldName;
            para[1] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            para[1].Value = TableName;
            para[2] = new SqlParameter("@Condition", SqlDbType.NVarChar);
            para[2].Value = Condition;
            try
            {
                dt = clsSQL.SPDataTable("CommonTagTime_CheckExist", para);
                if (Convert.ToInt32(dt.Rows[0][0]) > 0)
                {
                    flag = true;
                    dt.Dispose();
                }
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("clsData", "CheckExist(string FieldName, string TableName, string Condition)", ex.ToString());
            }
            finally
            {
                dt = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return flag;
        }

        public int Count(string FieldName, string TableName, string Condition)
        {
            int i = int.MinValue;

            clsSQL clsSQL = new clsSQL();
            DataTable dt = new DataTable();
            SqlParameter[] para = new SqlParameter[3];
            para[0] = new SqlParameter("@FieldName", SqlDbType.NVarChar);
            para[0].Value = FieldName;
            para[1] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            para[1].Value = TableName;
            para[2] = new SqlParameter("@Condition", SqlDbType.NVarChar);
            para[2].Value = Condition;
            try
            {
                dt = clsSQL.SPDataTable("CommonTagTime_Count", para);
                i = Convert.ToInt32(dt.Rows[0][0]);
                dt.Dispose();
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("clsData", "Count(string FieldName, string TableName, string Condition)", ex.ToString());
            }
            finally
            {
                dt = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return i;
        }

        public bool Delete(string TableName, string KeyField, string KeyValue)
        {
            bool flag = false;
            clsSQL clsSQL = new clsSQL();
            DataTable dt = new DataTable();
            SqlParameter[] para = new SqlParameter[4];

            try
            {
                para[0] = new SqlParameter("@TableName", SqlDbType.NVarChar);
                para[0].Value = TableName;
                para[1] = new SqlParameter("@KeyField", SqlDbType.NVarChar);
                para[1].Value = KeyField;
                para[2] = new SqlParameter("@KeyValue", SqlDbType.NVarChar);
                para[2].Value = KeyValue;
                para[3] = new SqlParameter("@@ROWCOUNT", SqlDbType.Int);
                para[3].Direction = ParameterDirection.ReturnValue;

                dt = clsSQL.SPDataTable("CommonTagTime_Delete", para);
                if (Convert.ToInt32(para[3].Value) > 0)
                {
                    flag = true;
                }

                dt.Dispose();
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("clsData", "Delete(string TableName, string KeyField, string KeyValue)", ex.ToString());
            }
            finally
            {
                dt = null;

                clsSQL.Dispose();
                clsSQL = null;
            }

            return flag;
        }

        public void FillDropDownList(ref DropDownList DropDownList, string Label, string TableName, string FieldValue, string FieldText, string Condition)
        {
            DropDownList.Items.Clear();
            clsSQL obj = new clsSQL();

            SqlParameter[] para = new SqlParameter[4];
            para[0] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            para[0].Value = TableName;
            para[1] = new SqlParameter("@FieldValue", SqlDbType.NVarChar);
            para[1].Value = FieldValue;
            para[2] = new SqlParameter("@FieldText", SqlDbType.NVarChar);
            para[2].Value = FieldText;
            para[3] = new SqlParameter("@Condition", SqlDbType.NVarChar);
            para[3].Value = Condition;

            DataTable dt = obj.SPDataTable("CommonTagTime_FillList", para);
            try
            {
                if (dt != null && dt.Rows.Count > 0 )
                {
                    DropDownList.DataTextField = FieldText;
                    DropDownList.DataValueField = FieldValue;
                    DropDownList.DataSource = dt;
                    DropDownList.DataBind();

                    dt.Dispose();
                }

                if (Label.Length > 0)
                {
                    DropDownList.Items.Insert(0, new ListItem(Label, ""));
                }
            }
            catch(Exception ex)
            {
                clsFile.WriteLog("clsData", "FillDropDownList(ref DropDownList DropDownList, string Note, string TableName, string FieldValue, string FieldText, string Condition)", ex.ToString());
            }
            finally
            {
                dt = null;
                obj.Dispose();
                obj = null;
            }
        }

        public void FillListBox(ref ListBox ListBox, string Label, string TableName, string FieldValue, string FieldText, string Condition)
        {
            ListBox.Items.Clear();

            clsSQL obj = new clsSQL();
            SqlParameter[] para = new SqlParameter[4];
            para[0] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            para[0].Value = TableName;
            para[1] = new SqlParameter("@FieldValue", SqlDbType.NVarChar);
            para[1].Value = FieldValue;
            para[2] = new SqlParameter("@FieldText", SqlDbType.NVarChar);
            para[2].Value = FieldText;
            para[3] = new SqlParameter("@Condition", SqlDbType.NVarChar);
            para[3].Value = Condition;

            DataTable dt = obj.SPDataTable("CommonTagTime_FillList", para);
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    ListBox.DataTextField = FieldText;
                    ListBox.DataValueField = FieldValue;
                    ListBox.DataSource = dt;
                    ListBox.DataBind();

                    dt.Dispose();
                }

                if (Label.Length > 0)
                {
                    ListBox.Items.Insert(0, new ListItem(Label, ""));
                }
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("clsData", "FillListBox(ref ListBox ListBox, string Label, string TableName, string FieldValue, string FieldText, string Condition)", ex.ToString());
            }
            finally
            {
                dt = null;
                obj.Dispose();
                obj = null;
            }
        }

        /*
        public DataTable GetList(string TableName, string FieldValue, string FieldText, string Condition)
        {
            clsSQL clsSQL = new clsSQL();
            DataTable dt = new DataTable();
            SqlParameter[] para = new SqlParameter[4];
            para[0] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            para[0].Value = TableName;
            para[1] = new SqlParameter("@FieldValue", SqlDbType.NVarChar);
            para[1].Value = FieldValue;
            para[2] = new SqlParameter("@FieldText", SqlDbType.NVarChar);
            para[2].Value = FieldText;
            para[3] = new SqlParameter("@Condition", SqlDbType.NVarChar);
            para[3].Value = Condition;
            try
            {
                dt = clsSQL.StoredProcedure("CommonTagTime_FillList", para);
                dt.Dispose();
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("clsCommonTagTimeData.GetList|" + ex);
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }

            return dt;

        }//end function

        public string GetString(string FieldName, string TableName, string KeyField, string KeyValue)
        {
            string Value = "";
            clsSQL clsSQL = new clsSQL();
            DataTable dt = new DataTable();
            SqlParameter[] para = new SqlParameter[4];
            para[0] = new SqlParameter("@FieldName", SqlDbType.NVarChar);
            para[0].Value = FieldName;
            para[1] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            para[1].Value = TableName;
            para[2] = new SqlParameter("@KeyField", SqlDbType.NVarChar);
            para[2].Value = KeyField;
            para[3] = new SqlParameter("@KeyValue", SqlDbType.NVarChar);
            para[3].Value = KeyValue;
            try
            {
                try
                {
                    dt = clsSQL.StoredProcedure("CommonTagTime_GetValue", para);
                    Value = dt.Rows[0][0].ToString();
                    dt.Dispose();
                }
                catch
                {
                    Value = "";
                }
            }
            finally
            {
                dt = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return Value;
        }

        public bool UpdateValue(string FieldName, string ValueUpdate, string TableName, string KeyField, string KeyValue)
        {
            bool flag;
            clsSQL clsSQL = new clsSQL();
            DataTable dt = new DataTable();
            SqlParameter[] para = new SqlParameter[6];
            para[0] = new SqlParameter("@FieldName", SqlDbType.NVarChar);
            para[0].Value = FieldName;
            para[1] = new SqlParameter("@ValueUpdate", SqlDbType.NVarChar);
            para[1].Value = ValueUpdate;
            para[2] = new SqlParameter("@TableName", SqlDbType.NVarChar);
            para[2].Value = TableName;
            para[3] = new SqlParameter("@KeyField", SqlDbType.NVarChar);
            para[3].Value = KeyField;
            para[4] = new SqlParameter("@KeyValue", SqlDbType.NVarChar);
            para[4].Value = KeyValue;
            para[5] = new SqlParameter("@@ROWCOUNT", SqlDbType.Int);
            para[5].Direction = ParameterDirection.ReturnValue;
            try
            {
                try
                {
                    dt = clsSQL.StoredProcedure("CommonTagTime_UpdateValue", para);
                    if (Convert.ToInt32(para[5].Value) == 1)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                    dt.Dispose();
                }
                catch
                {
                    flag = false;
                }
            }
            finally
            {
                dt = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return flag;
        }
        */
    }
}